package com.module.monitor.task;

import java.util.Date;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.module.admin.rest.cache.RestLogCache;
import com.module.admin.rest.pojo.RestInfo;
import com.module.admin.rest.pojo.RestStatDay;
import com.module.admin.rest.service.RestInfoService;
import com.module.admin.rest.service.RestStatDayService;
import com.system.comm.utils.FrameSpringBeanUtil;
import com.system.comm.utils.FrameTimeUtil;

/**
 * 接口统计
 * @author yuejing
 * @date 2016年10月22日 上午9:58:59
 * @version V1.0.0
 */
public class RestTask {
	
	private static final Logger LOGGER = Logger.getLogger(RestTask.class);

	public void run() {
		LOGGER.info("========================= 初始化接口的任务 - 成功 ===========================");
		ScheduledExecutorService service = Executors.newScheduledThreadPool(100);
		final RestInfoService restInfoService = FrameSpringBeanUtil.getBean(RestInfoService.class);
		final RestStatDayService restStatDayService = FrameSpringBeanUtil.getBean(RestStatDayService.class);
		final RestLogCache restLogCache = FrameSpringBeanUtil.getBean(RestLogCache.class);
		//线程，每隔30秒调用一次
		Runnable runnable = new Runnable() {
			public void run() {
				//获取今天的请求数等
				List<RestInfo> list = restInfoService.findAll();
				for (RestInfo ri : list) {
					Date statDate = FrameTimeUtil.getTime();
					Integer riId = ri.getRiId();
					RestStatDay rsd = new RestStatDay();
					rsd.setRsId(ri.getRsId());
					rsd.setRiId(riId);
					rsd.setStatDate(statDate);
					rsd.setHour1(restLogCache.get(riId, statDate, "01"));
					rsd.setHour2(restLogCache.get(riId, statDate, "02"));
					rsd.setHour3(restLogCache.get(riId, statDate, "03"));
					rsd.setHour4(restLogCache.get(riId, statDate, "04"));
					rsd.setHour5(restLogCache.get(riId, statDate, "05"));
					rsd.setHour6(restLogCache.get(riId, statDate, "06"));
					rsd.setHour7(restLogCache.get(riId, statDate, "07"));
					rsd.setHour8(restLogCache.get(riId, statDate, "08"));
					rsd.setHour9(restLogCache.get(riId, statDate, "09"));
					rsd.setHour10(restLogCache.get(riId, statDate, "10"));
					rsd.setHour11(restLogCache.get(riId, statDate, "11"));
					rsd.setHour12(restLogCache.get(riId, statDate, "12"));
					rsd.setHour13(restLogCache.get(riId, statDate, "13"));
					rsd.setHour14(restLogCache.get(riId, statDate, "14"));
					rsd.setHour15(restLogCache.get(riId, statDate, "15"));
					rsd.setHour16(restLogCache.get(riId, statDate, "16"));
					rsd.setHour17(restLogCache.get(riId, statDate, "17"));
					rsd.setHour18(restLogCache.get(riId, statDate, "18"));
					rsd.setHour19(restLogCache.get(riId, statDate, "19"));
					rsd.setHour20(restLogCache.get(riId, statDate, "20"));
					rsd.setHour21(restLogCache.get(riId, statDate, "21"));
					rsd.setHour22(restLogCache.get(riId, statDate, "22"));
					rsd.setHour23(restLogCache.get(riId, statDate, "23"));
					rsd.setHour0(restLogCache.get(riId, statDate, "00"));
					//访问次数
					rsd.setVisitNum(restLogCache.get(riId, statDate));
					restStatDayService.saveOrUpdate(rsd);
				}
			}
		};
		//第二个参数为首次执行的延时时间，第三个参数为定时执行的间隔时间
		service.scheduleAtFixedRate(runnable, 50, 30, TimeUnit.SECONDS);
	}

}
